var limsBatch = Gikam.getPageObject({

    getEditListColumns : function() {
        return [{
            checkbox: true
        }, {index: true}, {
            field: 'batchCode',
            title: 'T_LIMS_BATCH.BATCHCODE',
            width: 140,
            type: 'link',
            styleFormatter: function (row) {
                var color = null
                if (row.status === 'draft_recall' || row.status === 'draft_reject')
                    color = 'red'
                return {backgroundColor: color}
            },
        }, {
            field : 'batchType',
            title : 'T_LIMS_BATCH.BATCHTYPE',
            validators : [ 'notEmpty' ],
            type : 'select',
            firstBlank : false,
            category : 'batchType',
        }, {
            field : 'selected',
            title : 'T_LIMS_BATCH.SELECTED',
            validators : [ 'notEmpty' ],
            type : 'select',
            firstBlank : false,
            category : 'sys',
            param : {
                codeCategoryId_EQ : 'yesorno'
            },
        }, {
            field : 'createdByName',
            title : 'T_LIMS_BATCH.CREATEDBYNAME',
            width : 140,
        }, {
            field : 'createdTime',
            title : 'T_LIMS_BATCH.CREATEDTIME'
        } ];
    },

    getAuditListColumns : function() {
        return [{
            checkbox: true
        }, {index: true}, {
            field: 'batchCode',
            title: 'T_LIMS_BATCH.BATCHCODE',
            width: 140,
            type: 'link',
            styleFormatter: function (row) {
                var color = null
                if (row.status === 'draft_recall' || row.status === 'draft_reject')
                    color = 'red'
                return {backgroundColor: color}
            },
        }, {
            field : 'batchType',
            title : 'T_LIMS_BATCH.BATCHTYPE',
            validators : [ 'notEmpty' ],
            type : 'select',
            firstBlank : false,
            category : 'batchType',
        }, {
            field : 'selected',
            title : 'T_LIMS_BATCH.SELECTED',
            validators : [ 'notEmpty' ],
            type : 'select',
            firstBlank : false,
            category : 'sys',
            param : {
                codeCategoryId_EQ : 'yesorno'
            },
        }, {
            field : 'createdByName',
            title : 'T_LIMS_BATCH.CREATEDBYNAME',
            width : 140,
        }, {
            field : 'createdTime',
            title : 'T_LIMS_BATCH.CREATEDTIME'
        } ];
    },

    getChooseListColumns : function() {
        return [{
            index: true
        }, {
            field: 'batchCode',
            title: 'T_LIMS_BATCH.BATCHCODE',
            width: 140,
            type: 'link'
        }, {
            field : 'batchType',
            title : 'T_LIMS_BATCH.BATCHTYPE',
            type : 'select',
            firstBlank : false,
            category : 'batchType',
        }, /*{
            field : 'selected',
            title : 'T_LIMS_BATCH.SELECTED',
            type : 'select',
            firstBlank : false,
            category : 'sys',
            param : {
                codeCategoryId_EQ : 'yesorno'
            },
        },*/ {
            field : 'createdByName',
            title : 'T_LIMS_BATCH.CREATEDBYNAME',
            width : 140,
        }, {
            field : 'createdTime',
            title : 'T_LIMS_BATCH.CREATEDTIME'
        } ];
    },

    getBaseInfoFormFields : function() {
        return [{
            field : 'batchType',
            title : 'T_LIMS_BATCH.BATCHTYPE',
            preInsert : true,
            type : 'select',
            firstBlank : false,
            category : 'batchType',
        }, {
            field : 'selected',
            title : 'T_LIMS_BATCH.SELECTED',
            preInsert : true,
            type : 'select',
            firstBlank : false,
            category : 'sys',
            param : {
                codeCategoryId_EQ : 'yesorno'
            },
        }];
    },

    getSampleFormFields : function() {
        return [{
            field: 'sampleCode',
            title: 'T_LIMS_SAMPLE.SAMPLECODE',
            validators: ['notEmpty', /*'unique[/secure/baseline/samplemanage/samples/action/validate-unique]',*/'remote[/secure/baseline/samplemanage/samples/action/validate-generated-samplecode]'],
            preInsert: true,
        }, {
            field: 'sampleType',
            title: 'T_LIMS_SAMPLE.SAMPLETYPE',
            validators : [ 'notEmpty' ],
            type: 'choose',
            category: 'sampleType',
            single: true,
            onBeforeChoose : function(){
                return { filter_derivativeType : '0'}
            },
            onAfterChoose: function (rows, index) {
                if (rows) {
                    this.setData({
                        sampleType: rows[0].sampleTypeName,
                        sampleTypeCode: rows[0].sampleTypeCode
                    })
                }
            },
            onClean: function () {
                this.setData({
                    sampleType: '',
                    sampleTypeCode: ''
                })
            },
            preInsert : true,
        }, {
            field: 'sampleName',
            title: 'T_LIMS_SAMPLE.SAMPLENAME',
            validators : [ 'notEmpty' ],
            preInsert : true,
        }, {
            field: 'subjectId',
            title: 'T_LIMS_SAMPLE.SUBJECTID',
            validators : [ 'notEmpty' ],
            preInsert : true,
        }, {
            field: 'sampleSize',
            title: 'T_LIMS_SAMPLE.SAMPLESIZE',
            validators : [ 'notEmpty', 'integer', 'numRange(0,)' ],
            preInsert : true,
        }, {
            field: 'samplingPosition',
            title: 'T_LIMS_SAMPLE.SAMPLINGPOSITION',
            validators : [ 'notEmpty' ],
            type: 'select',
            firstBlank: false,
            search: true,
            category: 'sys',
            param: {
                codeCategoryId: 'samplingPosition'
            },
            preInsert : true,
        }, {
            field: 'identifyCode',
            title: 'T_LIMS_SAMPLE.IDENTIFYCODE',
            validators : [ 'notEmpty' ],
            preInsert : true,
        }, {
            field: 'birthday',
            title: 'T_LIMS_SAMPLE.BIRTHDAY',
            type: 'date',
            validators : [ 'notEmpty' ],
            dateRange: [, Gikam.DateUtils.formatter(new Date(), 'yyyy-MM-dd hh:mm:ss')],
            preInsert : true,
            onChange: function (field, value, index) {
                var age = limsBatch.getAge(value);
                this.setData({
                    age: age
                }, false);
            }
        }, {
            field: 'age',
            title: 'T_LIMS_SAMPLE.AGE',
            validators : [ 'notEmpty' ],
            preInsert : true,
        }, {
            field: 'gender',
            title: 'T_LIMS_SAMPLE.GENDER',
            validators : [ 'notEmpty' ],
            preInsert : true,
            type: 'select',
            firstBlank: false,
            search: true,
            category: 'sys',
            param: {
                codeCategoryId: 'gender'
            },
        }, {
            field: 'productNames',
            title: 'T_LIMS_PRODUCT_MAIN.PRODUCTNAME',
            preInsert : true,
            type: 'choose',
            category: 'product-choose',
            validators : [ 'notEmpty' ],
            colspan: 2,
            onBeforeChoose: function () {
                return {
                    status_EQ: '1',
                }
            },
            onAfterChoose: function (rows, index) {
                if (Gikam.isNotEmpty(rows)) {
                    this.setData({
                        productId: rows.map(e => e.id).join(','),
                        productNames: rows.map(e => e.productName).join(','),
                        productChange: '1'
                    })
                }
            },
            onClean: function (field, value, rowIndex) {
                this.setData({
                    productId: "",
                    productNames: "",
                })
            },
        }, {
            field: 'customerName',
            title: 'T_LIMS_SAMPLE.CUSTOMERCODE',
            type: 'choose',
            category: 'client-choose',
            single: true,
            onBeforeChoose : function(){
                return {
                    clientStatus_EQ: '1'
                }
            },
            onAfterChoose: function (rows, index) {
                if (rows) {
                    this.setData({
                        customerCode: '',
                        customerName: '',
                        department: ''
                    })
                    this.setData({
                        customerCode: rows[0].clientCode,
                        customerName: rows[0].clientName,
                        department: rows[0].office
                    })
                }
            },
            onClean: function () {
                this.setData({
                    customerCode: '',
                    customerName: '',
                    department: ''
                })
            },
            preInsert : true,
        }, {
        //     field: 'customerName',
        //     title: 'T_LIMS_SAMPLE.CUSTOMERNAME',
        //     readonly: true,
        //     // preInsert : true,
        // }, {
            field: 'department',
            title: 'T_LIMS_SAMPLE.DEPARTMENT',
            preInsert : true,
        }, {
            field: 'remark',
            title: 'T_LIMS_SAMPLE.REMARK',
            preInsert : true,
        }];
    },

    getSampleListColumns: function() {
        return [ {
            checkbox : true
        },{index: true}, {
            field: 'sampleCode',
            title: 'T_LIMS_SAMPLE.SAMPLECODE',
            validators : [ 'notEmpty' ],
            width: 140,
            type : 'link'
        }, {
            field: 'sampleType',
            title: 'T_LIMS_SAMPLE.SAMPLETYPE',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'ext$.productnames',
            title: 'T_LIMS_PRODUCT_MAIN.PRODUCTNAME',
            validators : [ 'notEmpty' ],
            width: 170,
        }, {
            field: 'sampleName',
            title: 'T_LIMS_SAMPLE.SAMPLENAME',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'subjectId',
            title: 'T_LIMS_SAMPLE.SUBJECTID',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'sampleSize',
            title: 'T_LIMS_SAMPLE.SAMPLESIZE',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'samplingPosition',
            title: 'T_LIMS_SAMPLE.SAMPLINGPOSITION',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'identifyCode',
            title: 'T_LIMS_SAMPLE.IDENTIFYCODE',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'birthday',
            title: 'T_LIMS_SAMPLE.BIRTHDAY',
            validators : [ 'notEmpty' ],
            type: 'date',
            width: 140,
        }, {
            field: 'age',
            title: 'T_LIMS_SAMPLE.AGE',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'gender',
            title: 'T_LIMS_SAMPLE.GENDER',
            validators : [ 'notEmpty' ],
            width: 140,
        }, {
            field: 'customerName',
            title: 'T_LIMS_SAMPLE.CUSTOMERCODE',
            width: 140,
        }, /*{
            field: 'customerName',
            title: 'T_LIMS_SAMPLE.CUSTOMERNAME',
            width: 140,
        },*/ {
            field: 'department',
            title: 'T_LIMS_SAMPLE.DEPARTMENT',
            width: 140,
        }, {
            field: 'remark',
            title: 'T_LIMS_SAMPLE.REMARK',
            width: 140,
        }]
    },

    getLogisticsListColumns : function() {
        return [{
            checkbox: true
        },{
            index: true
        }, {
            field: 'sampleCode',
            title: 'T_LIMS_SAMPLE.SAMPLECODE',
            width: 140,
            type : 'link'
        }, {
            field: 'temperatureLimits',
            title: 'T_LIMS_SAMPLE.TEMPERATURELIMITS',
        }, {
            field: 'senderName',
            title: 'T_LIMS_SAMPLE.SENDERNAME',
        }, {
            field: 'senderPhone',
            title: 'T_LIMS_SAMPLE.SENDERPHONE',
            width: 140,
        }, {
            field: 'courierNumber',
            title: 'T_LIMS_SAMPLE.COURIERNUMBER',
            width: 140,
        }, {
            field: 'courier',
            title: 'T_LIMS_SAMPLE.COURIER',
            width: 140,
        }, {
            field: 'senderRemark',
            title: 'T_LIMS_SAMPLE.SENDERREMARK',
        }]
    },

    getSampleLogisticsFormFields : function() {
        return [{
            field: 'temperatureLimits',
            title: 'T_LIMS_SAMPLE.TEMPERATURELIMITS',
        }, {
            field: 'senderName',
            title: 'T_LIMS_SAMPLE.SENDERNAME',
        }, {
            field: 'senderPhone',
            title: 'T_LIMS_SAMPLE.SENDERPHONE',
            validators: ['strLength[0,32]', 'phone'],
        }, {
            field: 'courierNumber',
            title: 'T_LIMS_SAMPLE.COURIERNUMBER',
        }, {
            field: 'courier',
            title: 'T_LIMS_SAMPLE.COURIER',
            type: 'select',
            firstBlank: false,
            search: true,
            category: 'sys',
            param: {
                codeCategoryId: 'courier'
            },
        }, {
            field: 'senderRemark',
            title: 'T_LIMS_SAMPLE.SENDERREMARK',
            colspan: 2,
        }]
    },


    getAge: function (str) {
        var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/);
        if (r == null) return false;

        var d = new Date(r[1], r[3] - 1, r[4]);
        var returnStr = "输入的日期格式错误！";

        if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {

            var date = new Date();
            var yearNow = date.getFullYear();
            var monthNow = date.getMonth() + 1;
            var dayNow = date.getDate();

            var largeMonths = [1, 3, 5, 7, 8, 10, 12], //大月， 用于计算天，只在年月都为零时，天数有效
                lastMonth = monthNow - 1 > 0 ? monthNow - 1 : 12,  // 上一个月的月份
                isLeapYear = false, // 是否是闰年
                daysOFMonth = 0;    // 当前日期的上一个月多少天

            if ((yearNow % 4 === 0 && yearNow % 100 !== 0) || yearNow % 400 === 0) {  // 是否闰年， 用于计算天，只在年月都为零时，天数有效
                isLeapYear = true;
            }

            if (largeMonths.indexOf(lastMonth) > -1) {
                daysOFMonth = 31;
            } else if (lastMonth === 2) {
                if (isLeapYear) {
                    daysOFMonth = 29;
                } else {
                    daysOFMonth = 28;
                }
            } else {
                daysOFMonth = 30;
            }

            var Y = yearNow - parseInt(r[1]);
            var M = monthNow - parseInt(r[3]);
            var D = dayNow - parseInt(r[4]);
            if (D < 0) {
                D = D + daysOFMonth; //借一个月
                M--;
            }
            if (M < 0) {  // 借一年 12个月
                Y--;
                M = M + 12; //
            }

            if (Y < 0) {
                returnStr = "出生日期有误！";

            } else if (Y === 0) {
                if (M === 0) {
                    returnStr = D + "day";
                } else {
                    returnStr = M + "months";
                }
            } else {
                returnStr = Y + " yr";
            }
        }

        return returnStr;

    },

    baseUrl : IFM_CONTEXT + '/secure/basemodule/business/batchs',
    batchSampleUrl : IFM_CONTEXT + '/secure/basemodule/business/batch-samples',
    batchSampleProductUrl : IFM_CONTEXT + '/secure/basemodule/business/batch-sample-products',
    sampleUrl : IFM_CONTEXT + '/secure/baseline/samplemanage/samples',

    batchSampleProductChooseListColumns: function ()  {
        return [{
            field: 'sampleCode',
            title: 'T_LIMS_SAMPLE.SAMPLECODE',
            width: 140,
        }, {
            field: 'ext$.sampletype',
            title: 'T_LIMS_SAMPLE.SAMPLETYPE',
            width: 140,
        }, {
            field: 'batchCode',
            title: 'T_LIMS_BATCH.BATCHCODE',
            width: 140,
        }, {
            field: 'productName',
            title: 'T_LIMS_PRODUCT_MAIN.PRODUCTNAME',
        }, {
            field: 'ext$.locationdesc',
            title: 'T_LIMS_LOCATION.LOCATIONDESC2',
            width: 140,
        }, {
            field: 'ext$.locations',
            title: 'T_LIMS_SAMPLE.LOCATIONS2',
            width: 140,
        }]
    },
});
